STATE_UNSTARTED = -1;
STATE_ENDED = 0;
STATE_PLAYING = 1;
STATE_PAUSED = 2;
STATE_BUFFERING = 3;
STATE_VIDEO_CUED = 5;

/************************************/
/* Youtube Player: Wraps one player */
/************************************/
function YouTubePlayer(id, playerContainer, player) {
	this.id = id;
	this.container = playerContainer;
	this.player = player;
	this.onFinishedCallback = null;
	this.onBufferingFinishedCallback = null;

	this.player.addEventListener("onStateChange", "onStateChange" + id);
}

YouTubePlayer.prototype.play = function() {
	this.player.unMute();
	this.player.playVideo();
	return this;
}
YouTubePlayer.prototype.hide = function() { this.container.addClass('invisible_player').removeClass('visible_player'); return this; }
YouTubePlayer.prototype.show = function() { this.container.addClass('visible_player').removeClass('invisible_player'); return this; }
YouTubePlayer.prototype.startBuffering = function() {
	this.player.mute()
	this.player.playVideo()
	this.player.pauseVideo()
	return this;
}
YouTubePlayer.prototype.onFinished = function(obj, method) {
	this.onFinishedCallback = function() { obj[method]() }
	return this;
}
YouTubePlayer.prototype.onBufferingFinished = function(obj, method) {
	this.onBufferingFinishedCallback = function() { obj[method]() }
	return this;
}
YouTubePlayer.prototype.stateChange = function(state) {
	if (state == STATE_ENDED && this.onFinishedCallback != null) {
		this.onFinishedCallback(state)
		this.onFinishedCallback = null;
	}
	return this;
}

YouTubePlayer.prototype.update = function() {
	if (this.player.getVideoBytesLoaded() == this.player.getVideoBytesTotal() && this.onBufferingFinishedCallback != null) {
		this.onBufferingFinishedCallback()
		this.onBufferingFinishedCallback = null;
	}
}

/****************************************************/
/* Youtube Player Collection: Wraps all the players */
/****************************************************/
function YouTubePlayerCollection() {
	this.players = {};
	this.count = 0;
	this.currentPlayer = 0;
	this.currentlyBufferingPlayer = 0;
}

YouTubePlayerCollection.prototype.addPlayer = function(id, container, player) {
	this.players[id] = new YouTubePlayer(id, container, player);
	this.count += 1;
}

YouTubePlayerCollection.prototype.onStateChange = function(id, state) {
	this.players[id].stateChange(state)
}

YouTubePlayerCollection.prototype.start = function() {
	this.players[0].show()
		.onFinished(this, "playerFinished")
		.onBufferingFinished(this, "bufferingFinished")
		.play()
}

YouTubePlayerCollection.prototype.playerFinished = function() {
	this.players[this.currentPlayer].hide()
	this.currentPlayer++

	if (this.currentPlayer < this.count) {
		this.players[this.currentPlayer].show()
		this.players[this.currentPlayer].onFinished(this, "playerFinished")
		this.players[this.currentPlayer].play()
	}
}

YouTubePlayerCollection.prototype.bufferingFinished = function() {
	this.currentlyBufferingPlayer++;

	if (this.currentlyBufferingPlayer < this.count) {
		this.players[this.currentlyBufferingPlayer].startBuffering()
			.onBufferingFinished(this, "bufferingFinished")
	}
}

YouTubePlayerCollection.prototype.update = function() {
	for (player_id in this.players) {
		this.players[player_id].update()
	}
}
